<template>
	<view class="container">
		<view class="header">
			<view class="title">余额记录</view>
			<view class="balance">
				<text class="balance-label">当前余额</text>
				<text class="balance-amount">{{userBalance}}元</text>
			</view>
		</view>
		
		<view class="filter-box">
			<view class="filter-item" @click="showTypeFilter">
				<text class="filter-text">{{typeFilterText}}</text>
				<text class="filter-arrow">▼</text>
			</view>
			<view class="filter-item" @click="showDatePicker">
				<text class="filter-text">{{dateFilterText}}</text>
				<text class="filter-arrow">▼</text>
			</view>
		</view>
		
		<scroll-view 
			scroll-y 
			class="balance-list" 
			:style="{height: scrollHeight + 'px'}"
			@scrolltolower="loadMore"
		>
			<view class="list-container">
				<view v-if="isLoading && balanceLogs.length === 0" class="loading-box">
					<text>加载中...</text>
				</view>
				
				<template v-else-if="balanceLogs.length > 0">
					<view 
						class="balance-item" 
						v-for="(item, index) in balanceLogs" 
						:key="item.logId"
					>
						<view class="item-left">
							<text class="item-title">{{getTypeText(item.type)}}</text>
							<text class="item-desc">{{item.remark}}</text>
							<text class="item-time">{{item.createTime}}</text>
							<text class="item-order" v-if="item.orderNo">订单号: {{item.orderNo}}</text>
						</view>
						<view class="item-right">
							<text class="item-amount" :class="{'amount-positive': parseFloat(item.amount) > 0, 'amount-negative': parseFloat(item.amount) < 0}">
								{{parseFloat(item.amount) > 0 ? '+' : ''}}{{item.amount}}
							</text>
							<text class="item-balance">余额: {{item.afterBalance}}</text>
						</view>
					</view>
				</template>
				
				<view v-else class="empty-box">
					<image class="empty-icon" src="/static/empty-data.png"></image>
					<text class="empty-text">暂无余额记录</text>
				</view>
				
				<view class="loading-more" v-if="isLoading && balanceLogs.length > 0">
					<text>加载更多数据...</text>
				</view>
				
				<view class="no-more" v-if="!hasMore && balanceLogs.length > 0">
					<text>没有更多数据了</text>
				</view>
			</view>
		</scroll-view>
		
		<!-- 类型筛选弹窗 -->
		<view class="popup-mask" v-if="showTypePopup" @click="closeTypePopup"></view>
		<view class="popup-content" v-if="showTypePopup">
			<view class="popup-title">选择交易类型</view>
			<view class="popup-list">
				<view 
					class="popup-item" 
					v-for="(item, index) in typeOptions" 
					:key="index" 
					:class="{'item-active': currentType === item.value}"
					@click="selectType(item)"
				>
					<text>{{item.label}}</text>
					<image v-if="currentType === item.value" class="item-check" src="/static/icon-check.png"></image>
				</view>
			</view>
		</view>
		
		<!-- 日期筛选弹窗 -->
		<view class="popup-mask" v-if="showDatePopup" @click="closeDatePopup"></view>
		<view class="popup-content date-popup" v-if="showDatePopup">
			<view class="popup-title">选择日期范围</view>
			<view class="date-range">
				<view class="date-item">
					<text class="date-label">开始日期</text>
					<picker mode="date" :value="tempBeginTime" @change="onBeginTimeChange">
						<view class="picker-box">
							<text class="picker-text">{{tempBeginTime || '请选择'}}</text>
							<text class="date-icon">📅</text>
						</view>
					</picker>
				</view>
				<view class="date-item">
					<text class="date-label">结束日期</text>
					<picker mode="date" :value="tempEndTime" @change="onEndTimeChange">
						<view class="picker-box">
							<text class="picker-text">{{tempEndTime || '请选择'}}</text>
							<text class="date-icon">📅</text>
						</view>
					</picker>
				</view>
			</view>
			<view class="popup-btns">
				<button class="btn btn-cancel" @click="clearDateFilter">清除</button>
				<button class="btn btn-confirm" @click="confirmDateFilter">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '../../../api/api.js';
	
	export default {
		data() {
			return {
				scrollHeight: 0,
				userBalance: '0.00',
				// 余额记录列表
				balanceLogs: [],
				// 分页参数
				pageNum: 1,
				pageSize: 10,
				total: 0,
				hasMore: true,
				isLoading: false,
				
				// 筛选参数
				currentType: '',  // 当前选择的类型
				beginTime: '',    // 开始日期
				endTime: '',      // 结束日期
				tempBeginTime: '', // 临时开始日期
				tempEndTime: '',   // 临时结束日期
				
				// 弹窗控制
				showTypePopup: false,
				showDatePopup: false,
				
				// 筛选选项
				typeOptions: [
					{ label: '全部交易', value: '' },
					{ label: '充值', value: '1' },
					{ label: '消费', value: '2' },
					{ label: '退款', value: '3' },
					{ label: '提现', value: '4' },
					{ label: '系统调整', value: '5' }
				]
			}
		},
		computed: {
			// 类型筛选显示文本
			typeFilterText() {
				if (!this.currentType) {
					return '全部交易';
				}
				const selectedType = this.typeOptions.find(item => item.value === this.currentType);
				return selectedType ? selectedType.label : '全部交易';
			},
			// 日期筛选显示文本
			dateFilterText() {
				if (this.beginTime && this.endTime) {
					return `${this.beginTime} 至 ${this.endTime}`;
				}
				return '时间筛选';
			}
		},
		onLoad() {
			this.initPage();
			this.getUserInfo();
			this.getBalanceLogs();
		},
		methods: {
			// 初始化页面
			initPage() {
				const systemInfo = uni.getSystemInfoSync();
				// 计算列表高度 = 屏幕高度 - 顶部状态栏 - 导航栏高度 - 筛选栏高度 - 底部安全区
				this.scrollHeight = systemInfo.windowHeight - 44 - 50 - 60 - 20;
			},
			// 获取用户信息
			async getUserInfo() {
				try {
					const res = await api.user.getCurrentUserInfo();
					if (res.code === 200 && res.data) {
						this.userBalance = res.data.balance || '0.00';
					}
				} catch (error) {
					console.error('获取用户信息失败', error);
				}
			},
			// 获取余额记录
			async getBalanceLogs() {
				if (this.isLoading) return;
				
				this.isLoading = true;
				try {
					// 构建查询参数
					const params = {
						pageNum: this.pageNum,
						pageSize: this.pageSize
					};
					
					// 添加类型筛选
					if (this.currentType) {
						params.type = this.currentType;
					}
					
					// 添加日期筛选 - 使用正确的格式
					if (this.beginTime) {
						params['params[beginTime]'] = this.beginTime;
					}
					if (this.endTime) {
						params['params[endTime]'] = this.endTime;
					}
					
					console.log("请求参数:", params);
					const res = await api.balance.getBalanceLogs(params);
					
					if (res.code === 200) {
						if (this.pageNum === 1) {
							this.balanceLogs = res.rows || [];
						} else {
							this.balanceLogs = [...this.balanceLogs, ...(res.rows || [])];
						}
						
						this.total = res.total || 0;
						this.hasMore = this.balanceLogs.length < this.total;
					} else {
						uni.showToast({
							title: res.msg || '获取余额记录失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('获取余额记录失败', error);
					uni.showToast({
						title: '获取余额记录失败',
						icon: 'none'
					});
				} finally {
					this.isLoading = false;
				}
			},
			// 加载更多
			loadMore() {
				if (this.hasMore && !this.isLoading) {
					this.pageNum++;
					this.getBalanceLogs();
				}
			},
			// 重置列表
			resetList() {
				this.pageNum = 1;
				this.hasMore = true;
				this.balanceLogs = [];
				this.getBalanceLogs();
			},
			// 获取交易类型文本
			getTypeText(type) {
				const typeMap = {
					'1': '充值',
					'2': '消费',
					'3': '退款',
					'4': '提现',
					'5': '系统调整'
				};
				return typeMap[type] || '未知类型';
			},
			
			// 显示类型筛选弹窗
			showTypeFilter() {
				this.showTypePopup = true;
			},
			// 关闭类型筛选弹窗
			closeTypePopup() {
				this.showTypePopup = false;
			},
			// 选择类型
			selectType(item) {
				this.currentType = item.value;
				this.closeTypePopup();
				this.resetList();
			},
			
			// 显示日期选择器
			showDatePicker() {
				// 初始化临时日期为当前已选择的日期
				this.tempBeginTime = this.beginTime || '';
				this.tempEndTime = this.endTime || '';
				this.showDatePopup = true;
			},
			// 关闭日期选择器
			closeDatePopup() {
				this.showDatePopup = false;
			},
			// 开始日期变化
			onBeginTimeChange(e) {
				this.tempBeginTime = e.detail.value;
				console.log('开始日期已选择:', this.tempBeginTime);
			},
			// 结束日期变化
			onEndTimeChange(e) {
				this.tempEndTime = e.detail.value;
				console.log('结束日期已选择:', this.tempEndTime);
			},
			// 确认日期筛选
			confirmDateFilter() {
				// 检查日期是否有效
				if (!this.tempBeginTime && this.tempEndTime) {
					uni.showToast({
						title: '请选择开始日期',
						icon: 'none'
					});
					return;
				}
				if (this.tempBeginTime && !this.tempEndTime) {
					uni.showToast({
						title: '请选择结束日期',
						icon: 'none'
					});
					return;
				}
				
				// 赋值并更新
				this.beginTime = this.tempBeginTime;
				this.endTime = this.tempEndTime;
				console.log('确认日期筛选:', this.beginTime, this.endTime);
				this.closeDatePopup();
				this.resetList();
			},
			// 清除日期筛选
			clearDateFilter() {
				this.beginTime = '';
				this.endTime = '';
				this.tempBeginTime = '';
				this.tempEndTime = '';
				this.closeDatePopup();
				this.resetList();
			}
		}
	}
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		min-height: 100vh;
	}
	
	.header {
		background: linear-gradient(135deg, #FF0000, #FF4D4D);
		padding: 30rpx 20rpx;
		color: #fff;
	}
	
	.title {
		font-size: 34rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	
	.balance {
		display: flex;
		flex-direction: column;
	}
	
	.balance-label {
		font-size: 28rpx;
		margin-bottom: 10rpx;
		opacity: 0.8;
	}
	
	.balance-amount {
		font-size: 50rpx;
		font-weight: bold;
	}
	
	.filter-box {
		display: flex;
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}
	
	.filter-item {
		display: flex;
		align-items: center;
		margin-right: 30rpx;
		padding: 10rpx 20rpx;
		background-color: #f5f5f5;
		border-radius: 30rpx;
	}
	
	.filter-text {
		font-size: 26rpx;
		color: #333;
		margin-right: 10rpx;
	}
	
	.filter-arrow {
		width: 24rpx;
		height: 24rpx;
		font-size: 20rpx;
		color: #666;
	}
	
	.balance-list {
		background-color: #f5f5f5;
		padding: 0 20rpx;
		box-sizing: border-box;
		width: 100%;
	}
	
	.list-container {
		padding-bottom: 30rpx;
		width: 100%;
	}
	
	.balance-item {
		display: flex;
		justify-content: space-between;
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		box-sizing: border-box;
		width: 100%;
	}
	
	.item-left {
		flex: 1;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	
	.item-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	
	.item-desc {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 10rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	
	.item-time {
		font-size: 24rpx;
		color: #999;
		margin-bottom: 6rpx;
	}
	
	.item-order {
		font-size: 24rpx;
		color: #999;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	
	.item-right {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		justify-content: center;
		min-width: 160rpx;
		margin-left: 20rpx;
	}
	
	.item-amount {
		font-size: 34rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}
	
	.amount-positive {
		color: #FF0000;
	}
	
	.amount-negative {
		color: #333;
	}
	
	.item-balance {
		font-size: 24rpx;
		color: #999;
	}
	
	.empty-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
	}
	
	.empty-icon {
		width: 180rpx;
		height: 180rpx;
		margin-bottom: 20rpx;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999;
	}
	
	.loading-box {
		text-align: center;
		padding: 30rpx 0;
		color: #999;
		font-size: 28rpx;
	}
	
	.loading-more, .no-more {
		text-align: center;
		padding: 20rpx 0;
		color: #999;
		font-size: 26rpx;
	}
	
	/* 弹窗样式 */
	.popup-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 100;
	}
	
	.popup-content {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		padding: 30rpx;
		z-index: 101;
		max-height: 70vh;
		overflow-y: auto;
	}
	
	.popup-title {
		font-size: 32rpx;
		font-weight: bold;
		text-align: center;
		margin-bottom: 30rpx;
		color: #333;
	}
	
	.popup-list {
		padding-bottom: 30rpx;
	}
	
	.popup-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 25rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
		font-size: 30rpx;
		color: #333;
	}
	
	.item-active {
		color: #FF0000;
	}
	
	.item-check {
		width: 40rpx;
		height: 40rpx;
	}
	
	/* 日期筛选样式 */
	.date-range {
		padding: 20rpx 0;
	}
	
	.date-item {
		margin-bottom: 20rpx;
	}
	
	.date-label {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 10rpx;
		display: block;
	}
	
	.picker-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border: 1rpx solid #ddd;
		padding: 15rpx 20rpx;
		border-radius: 8rpx;
	}
	
	.picker-text {
		font-size: 28rpx;
		color: #333;
	}
	
	.date-icon {
		width: 36rpx;
		height: 36rpx;
		font-size: 32rpx;
		line-height: 36rpx;
	}
	
	.popup-btns {
		display: flex;
		margin-top: 30rpx;
	}
	
	.btn {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 30rpx;
		border-radius: 8rpx;
	}
	
	.btn-cancel {
		background-color: #f5f5f5;
		color: #666;
		margin-right: 20rpx;
	}
	
	.btn-confirm {
		background-color: #FF0000;
		color: #fff;
	}
</style> 